﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using WinUtilities.Class;

namespace WinUtilities.Form
{
    public partial class FormSearchResult : DevExpress.XtraEditors.XtraForm
    {
        Collection<SearchItem> _displayItems = new Collection<SearchItem>();
        private bool haveResult = true;
        public Collection<SearchItem> DisplayItems
        {
            get { return _displayItems; }
            set
            {
                _displayItems = value;
                grvSearchResult.ForceInitialize();
                gridView1.Columns.Clear();
                GridColumn[] listColumn = new GridColumn[_displayItems.Count];
                for (int i = 0; i < _displayItems.Count; i++)
                {
                    GridColumn column = new GridColumn();
                    column.Caption = _displayItems[i].Name;
                    column.FieldName = _displayItems[i].FieldName;
                    column.Name = "cln" + _displayItems[i].FieldName;
                    column.OptionsColumn.AllowEdit = false;
                    if (i != _displayItems.Count - 1 && _displayItems[i].FieldLength > 0)
                        column.Width = _displayItems[i].FieldLength * 5;
                    listColumn[i] = column;
                }
                gridView1.Columns.AddRange(listColumn);
                foreach (GridColumn column in gridView1.Columns)
                {
                    column.Visible = true;
                }
            }
        }

        [DefaultValue(true)]
        public bool HaveResult { get { return haveResult; } set { haveResult = value; } }

        public FormSearchResult()
        {
            InitializeComponent();
            grvSearchResult.MouseDoubleClick += grvSearchResult_MouseDoubleClick;
            grvSearchResult.KeyDown += grvSearchResult_KeyDown;
            KeyDown += FormSearchResult_KeyDown;
        }

        void FormSearchResult_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                HaveResult = false;
                Close();
            }
        }

        void grvSearchResult_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                grvSearchResult_MouseDoubleClick(sender, null);
            }
            if (e.KeyCode == Keys.Escape)
            {
                HaveResult = false;
                Close();
            }
        }

        void grvSearchResult_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (DisplaySearchResult != null)
            {
                DisplaySearchResult(gridView1.GetRow(gridView1.FocusedRowHandle));
                Close();
            }
            else
                MessageBox.Show("Not implement DisplaySearchResult method");
        }

        public delegate void FillData(object resultRow);

        public event FillData DisplaySearchResult;

        public void FillSearchResult(IBindingListView searchResult)
        {
            grvSearchResult.DataSource = searchResult;
        }
    }
}